package com.dongqi.myblog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dongqi.myblog.po.TBlog;
import com.dongqi.myblog.service.TBlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dongqi
 * @since 2020-08-03
 */
@Controller
@RequestMapping("/blog")
public class TBlogController {

    @Autowired
    private TBlogService blogService;

    /**
     * 根据id获取blog
     */
    @GetMapping("/{id}")
    public String search(@PathVariable Long id, Model model){
        TBlog blog = blogService.getAndConvert(id);
        model.addAttribute("blog",blog);
        return "blog";
    }

    /**
     * blog归档（年份）
     */
    @GetMapping("/archives")
    public String archives(Model model){
        Map<String, List<TBlog>> map = blogService.archiveBlog();
        Long count = blogService.countBlog();
        model.addAttribute("archivesMap",map);
        model.addAttribute("count",count);
        return "archives";
    }

    /**
     * 跳转至时间轴页面
     */
    @GetMapping("/timer")
    public String timer(Model model){
        QueryWrapper<TBlog> wrapper = new QueryWrapper<>();
        wrapper.eq("published",true)
                .orderByDesc("create_time");
        List<TBlog> list = blogService.list(wrapper);
        model.addAttribute("blogs",list);
        return "timer";
    }

}

